#include "stdafx.h"
#include "Font.h"
#include "SGD Wrappers\SGD_GraphicsManager.h"
#include <ctype.h>
#include <cassert>


void Font::Initialize(void)
{	
	m_hImage = SGD::GraphicsManager::GetInstance()->LoadTexture(
		
		L"pictures/font3.0.bmp", {1,0,0,0});

	m_nCharWidth = 72;
	m_nCharHeight = 98;
	m_nNumRows = 10;
	m_nNumCols = 14;
	m_cFirstChar = ' ';
	m_bOnlyUppercase = false;
}

void Font::Terminate(void)
{	
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
}


void Font::Draw(const char* output, SGD::Point position,
	float scale, SGD::Color color) const
{
	// Validate the image
	assert(m_hImage != SGD::INVALID_HANDLE
		&& "BitmapFont::Draw - image was not loaded");

	// Validate the parameter
	assert(output != nullptr
		&& "BitmapFont::Draw - string cannot be null");


	// Is this string invisible?
	if (output[0] == '\0'			// string is empty?
		|| scale == 0.0f			// scale is invisible?
		|| color.alpha == 0)		// color is invisible?
		return;


	// Store the starting X position for newlines
	float colStart = position.x;

	// Iterate through the characters in the string
	for (int i = 0; output[i]; i++)
	{
		// Get the current character
		char ch = output[i];


		// Check for whitespace
		if (ch == ' ')
		{
			// Move to the next position
			position.x += (m_nCharWidth - 15) * scale;
			continue;
		}
		else if (ch == '\n')
		{
			// Move to the next row
			position.y += m_nCharHeight * scale;
			position.x = colStart;
			continue;
		}
		else if (ch == '\t')
		{
			// Calculate the number of pixels from the start
			float pixels = position.x - colStart;

			// Calculate the number of characters from the start
			int chars = int(pixels / (m_nCharWidth * scale));

			// Calculate the number of characters to add
			// to get a 4-space alignment
			int spaces = 4 - (chars % 4);


			// Move to the next position
			position.x += spaces * (m_nCharWidth * scale);
			continue;
		}	

		if (ch == 'A')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 360, 200, 400, 280 }, 0.0f, {},
				color, { scale, scale });
			
			position.x += 45 * scale;
		}
		else if (ch == 'B')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 430, 210, 480, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'C')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 500, 210, 550, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'D')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 570, 210, 630, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;

		}
		else if (ch == 'E')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 640, 210, 700, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;

		}
		else if (ch == 'F')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 720, 210, 770, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'G')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 790, 210, 840, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'H')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 860, 210, 920, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'I')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 930, 210, 960, 280 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'J')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 0, 310, 30, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'K')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 70, 310, 130, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'L')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 140, 310, 180, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 'M')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 210, 310, 287, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 82 * scale;
		}
		else if (ch == 'N')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 290, 310, 340, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'O')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 360, 310, 410, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'P')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 430, 310, 480, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'Q')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 500, 310, 550, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'R')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 570, 310, 630, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'S')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 640, 310, 690, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'T')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 720, 310, 770, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'U')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 790, 310, 850, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'V')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 860, 310, 920, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'W')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 930, 310, 1010, 380 }, 0.0f, {},
				color, { scale, scale });

			position.x += 85 * scale;
		}
		else if (ch == 'X')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 0, 410, 50, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 75 * scale;
		}
		else if (ch == 'Y')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 70, 410, 130, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == 'Z')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 140, 410, 190, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'a')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 640, 410, 690, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'b')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 720, 410, 760, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 'c')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 790, 410, 830, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 'd')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 860, 410, 910, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'e')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 930, 410, 980, 480 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'f')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 0, 510, 30, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'g')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 70, 510, 120, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'h')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 140, 510, 190, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'i')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 210, 510, 240, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'j')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 280, 510, 310, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'k')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 350, 510, 400, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'l')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, SGD::Point{ position.x , position.y }, SGD::Rectangle{ 430, 510, 450, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 25 * scale;
		}		
		else if (ch == 'm')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, {position.x, position.y + 10}, SGD::Rectangle{ 500, 520, 570, 580 }, 0.0f, {},
				color, { scale, scale });

			position.x += 75 * scale;
		}	
		else if (ch == 'n')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 570, 510, 620, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'o')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, { position.x, position.y + 15 }, SGD::Rectangle{ 640, 530, 690, 580 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'p')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 710, 510, 760, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'q')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 790, 510, 840, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'r')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 860, 510, 900, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 's')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 930, 510, 970, 590 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 't')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 0, 610, 30, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == 'u')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 70, 610, 120, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'v')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 140, 610, 190, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == 'w')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 210, 610, 280, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 75 * scale;
		}
		else if (ch == 'x')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 290, 610, 330, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 'y')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 360, 610, 400, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == 'z')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 430, 610, 470, 690 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == ':')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 860, 120, 890, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == '.')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, { position.x, position.y + 20 }, SGD::Rectangle{ 360, 830, 390, 860 }, 0.0f, {},
				color, { scale, scale });

			position.x += 35 * scale;
		}
		else if (ch == '0')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage,position , SGD::Rectangle{ 140, 110, 190, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == '1')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 210, 110, 250, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 45 * scale;
		}
		else if (ch == '2')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 280, 110, 330, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == '3')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 350, 110, 410, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == '4')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 430, 110, 480, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == '5')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 500, 110, 550, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == '6')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 570, 110, 620, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}
		else if (ch == '7')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 640, 110, 700, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == '8')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 710, 110, 770, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 65 * scale;
		}
		else if (ch == '9')
		{
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hImage, position, SGD::Rectangle{ 790, 110, 840, 180 }, 0.0f, {},
				color, { scale, scale });

			position.x += 55 * scale;
		}		
	}
}